package com.wang.springframework.context.support;




import com.wang.springframework.beans.BeansException;
import com.wang.springframework.beans.factory.ConfigurableListableBeanFactory;
import com.wang.springframework.beans.factory.config.BeanFactoryPostProcessor;
import com.wang.springframework.beans.factory.config.BeanPostProcessor;
import com.wang.springframework.context.ConfigurableApplicationContext;
import com.wang.springframework.core.io.DefaultResourceLoader;

import java.util.Map;

/**
 * @author wangjianhua
 * @Description
 * @date 2021/8/26/026 13:52
 **/
public abstract class AbstractApplicationContext  extends DefaultResourceLoader implements ConfigurableApplicationContext {


    /**
     * 刷新应用上下文 开始就是加载
     * @throws BeansException 异常
     */
    @Override
    public void refresh() throws BeansException {
        //1. 创建BeanFactory 并加载BeanDefinition
        refreshBeanFactory();

        //2.获取BeanFactory
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();

        //3.添加ApplicationContextAwareProcessor 让继承自ApplicationContextAware的bean对象都能感知到所属的ApplicationContext
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

        //4.在bean实例化之前 执行BeanPostProcessor (Invoke factory processors registered as beans in the context.)
        invokeBeanFactoryPostProcessors(beanFactory);

        //5.BeanPostProcessor需要先于其他bean进行注册操作
        registerBeanPostProcessors(beanFactory);

        //6.提前实例化单例对象
        beanFactory.preInstantiateSingletons();
    }

    /**
     * 刷新BeanFactory
     * @throws BeansException 异常
     */
    protected abstract void refreshBeanFactory() throws BeansException;

    /**
     * 获取bean的适配接口工厂
     * @return ConfigurableListableBeanFactory
     * @throws BeansException 异常
     */
    protected abstract ConfigurableListableBeanFactory getBeanFactory() throws BeansException;

    /**
     * 该方法提供了实现BeanPostProcessor接口的修改方法
     * 也就是说 它有修改BeanDefinition的能力  作用在bean实例化之前
     * @param beanFactory beanFactory
     */
    private void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory){
        Map<String, BeanFactoryPostProcessor> beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
        for (BeanFactoryPostProcessor beanFactoryPostProcessor : beanFactoryPostProcessorMap.values()) {
            beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
        }
    }

    /**
     * 遍历并把beanPostProcessor的实现加入工厂
     * @param beanFactory 工厂
     */
    private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory){
        Map<String, BeanPostProcessor> beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class);
        for (BeanPostProcessor beanPostProcessor : beanPostProcessorMap.values()) {
            beanFactory.addBeanPostProcessor(beanPostProcessor);
        }
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
        return getBeanFactory().getBeansOfType(type);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return getBeanFactory().getBean(name, args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return getBeanFactory().getBean(name, requiredType);
    }

    @Override
    public Object getBean(String name) throws BeansException {
        return getBeanFactory().getBean(name);
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return getBeanFactory().getBeanDefinitionNames();
    }

    @Override
    public void registerShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread(this::close));
    }

    @Override
    public void close() {
        getBeanFactory().destroySingletons();
    }
}
